import http from '../utils/http'
import constant from '../utils/constant'
import {sendCancelOrderMessage} from '../utils/sms'

export default {
  name: 'order-detail',
  data () {
    return {
      orderId: null,
      orderData: null
    }
  },
  created () {
    // 组件创建完后获取数据，
    // 此时 data 已经被 observed 了
    if (this.$route.params.orderId) {
      this.fetchOrderData()
    }
  },
  methods: {
    fetchOrderData() {
      this.orderId = this.$route.params.orderId
      http.get(constant.orderDetail + this.orderId, null, resp => {
        this.orderData = resp
      }, function (resp) {
      }, error => {
        this.$message.error({
          message: error.message,
          align: 'center',
          showClose: false,
          duration: 3000
        })
      })
    },
    goBack(step) {
      this.$router.go(step)
    },
    cancelOrderConfim() {
      this.$bvModal.msgBoxConfirm('确定取消订单吗？', {
        centered: true
      }).then(value => {
        if (value) {
          // 取消订单
          debugger
          let loading = this.$message.loading({
            message: '正在取消订单...',
            align: 'center'
          })
          http.post(constant.orderCancel(this.orderData.order.id), {'_method': 'PATCH'}, data => {
            loading.close()
            this.$message.success({
              message: '订单已取消',
              duration: 1500,
              align: 'center',
              onClose: () => {
                this.$router.push({name: 'profile', params: {refresh: true}})
              }
            })
            sendCancelOrderMessage(this.$store.getters.getMobile, this.orderData.order.id)
          }, data => {
            loading.close()
            this.$message.error(data.message)
          })
        }
      })
    }
  },
  watch: {
    '$route' (to, from) {
      // 对路由变化作出响应...
      if (to.params.orderId) {
        this.fetchOrderData()
      }
    }
  }
}
